package com.ideal.manage.esms.service.system;

import com.ideal.manage.esms.bean.DTO.ZtreeDto;
import com.ideal.manage.esms.bean.sys.*;
import com.ideal.manage.esms.config.shiro.MyShiroRealm;
import com.ideal.manage.esms.repository.framework.MySpecification;
import com.ideal.manage.esms.repository.framework.SpecificationOperator;
import com.ideal.manage.esms.repository.system.RoleRepository;
import com.ideal.manage.esms.util.HttpRequests;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
@CacheConfig(cacheNames = "roleCache")
public class RoleService {
    @Autowired
    private RoleRepository roleRepository;

    @Resource
    private ParameterService parameterService;
    @Resource
    private MenuService menuService;
    @Resource
    private RoleMenuService roleMenuService;


    /**
     * 分页 根据创建时间排序
     *
     * @param pageNum
     * @return
     */
    @Cacheable(value = "rolePage")
    public Page<Role> findAll(int pageNum, HttpServletRequest request) {
        List<SpecificationOperator> operators = HttpRequests.getParametersStartingWith(request, "Q_");
        Object value = null;
        String oper = "";
        String key="";
        List<SpecificationOperator> operatorList =new ArrayList<>();
        for (SpecificationOperator operator : operators) {
            if (operator.getKey().equals("type")) {
                value = operator.getValue();
                oper = operator.getOper();
                operatorList.add(new SpecificationOperator("type.id", value, oper));
            }else{
                value = operator.getValue();
                oper = operator.getOper();
                key= operator.getKey();
                operatorList.add(new SpecificationOperator(key, value, oper));
            }
        }

        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        SpecificationOperator operator = new SpecificationOperator("delFlag", "0", "EQ");
        operatorList.add(operator);
        MySpecification<Role> mySpecifications = new MySpecification<>(operatorList);
        Pageable pageable = new PageRequest(pageNum, 15, sort);
        Page<Role> page = roleRepository.findAll(mySpecifications, pageable);
        return page;
    }


    /**
     * 保存
     *
     * @param role
     */
    @CacheEvict(value = "rolePage", allEntries = true)
    public Role saveOne(Role role) {
        MyShiroRealm.ShiroUser shiroUser = (MyShiroRealm.ShiroUser) SecurityUtils.getSubject().getPrincipal();
        User user = new User();
        user.setId(shiroUser.getId());
        role.setCreateUser(user);
        role.setDelFlag(0L);
        role.setCreateTime(new Date());
        Role role1 = roleRepository.save(role);
        return role1;
    }

    /**
     * 根据ID 获取role
     *
     * @param id
     * @return
     */
    public Role findById(String id) {
        Role role = roleRepository.findById(id);
        return role;
    }

    @CacheEvict(value = "rolePage", allEntries = true)
    public void delRole(String[] ids) {
        List<Role> roles = roleRepository.findAll(Arrays.asList(ids));
        for (Role role : roles) {
            role.setDelFlag(1L);
        }
        roleRepository.save(roles);
    }

    public List<Role> findAllRoles() {
        List<Role> roles = roleRepository.findAllRoles();
        return roles;
    }

    /**
     * 获取指定身份的角色
     *
     * @param name
     */
    public List<Role> findByType(String name) {
        Parameter parameter = parameterService.findByCodeAndRemark("10000", name);
        List<Role> roles = roleRepository.findByType(parameter);
        return roles;
    }

    public List<ZtreeDto> roleAuthorization(String roleId) {
        if (roleId == null) {
            return null;
        }
        List<ZtreeDto> ztreeDtos = new ArrayList<>();
        List<Menu> menus = menuService.findAllMenu();
        for (Menu menu : menus) {
            RoleMenu roleMenu = roleMenuService.findByRoleAndMenu(roleId, menu.getId());
            if (roleMenu == null) {
                ztreeDtos.add(new ZtreeDto(menu.getId(), menu.getParentMenu() == null ? "" : menu.getParentMenu().getId(), menu.getTitle(), false, false));
            } else {
                ztreeDtos.add(new ZtreeDto(menu.getId(), menu.getParentMenu() == null ? "" : menu.getParentMenu().getId(), menu.getTitle(), false, true));
            }
        }
        return ztreeDtos;
    }
}
